import com.ZYHsheep.dao.*;
import com.ZYHsheep.entity.*;

import java.util.List;
import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);
    static SheepDao sd = new SheepDao();
    static CastrateDao cd = new CastrateDao();
    static FixhoofDao fd = new FixhoofDao();
    static final UserDao ud = new UserDao();
    static final BodyMeasureDao bd = new BodyMeasureDao();

    public static void main(String[] args) {
        while (true) {
            System.out.println("\n=== 羊只管理系统 ===");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.退出");
            System.out.print("请输入你的操作：");
            String opt = sc.nextLine();
            switch (opt) {
                case "1":
                    login();
                    break;
                case "2":
                    register();
                    break;
                case "3":
                    System.out.println("你确定要退出吗(Y/N)");
                    String s = sc.nextLine().trim();
                    if (s.equals("Y")) {
                        return;
                    } else if (s.equals("N")) {
                        break;
                    } else {
                        System.out.println("输入无效");
                    }
                    break;
                default:
                    System.out.println("输入有误，请重试！");
            }
        }
    }

    //登录
    public static void login() {
        User currentUser = null;
        while (true) {
            System.out.print("用户名：");
            String username = sc.nextLine();
            System.out.print("密  码：");
            String password = sc.nextLine();

            currentUser = ud.login(username, password);
            if (currentUser != null) {
                System.out.println("登录成功");
                break;
            }
            System.out.println("用户名或密码错误，请重新输入！");
        }
        showMainMenu();
    }

    //注册
    public static void register() {
        System.out.print("请输入新用户名：");
        String username = sc.nextLine();
        System.out.print("请输入新密码：");
        String password = sc.nextLine();

        if (username.isBlank() || password.isBlank()) {
            System.out.println("用户名或密码不能为空");
            return;
        }

        if (ud.findByUsername(username) != null) {
            System.out.println("注册失败,用户名已存在");
            return;
        }

        User u = new User(username, password);
        if (ud.insert(u) > 0) {
            System.out.println("注册成功");
        } else {
            System.out.println("注册失败");
        }
    }

    public static void showMainMenu() {
        System.out.println("\n=== 羊只管理系统 ===");
        System.out.println("1.查看羊只信息");
        System.out.println("2.新增羊只信息");
        System.out.println("3.修改羊只信息");
        System.out.println("4.羊只去势");
        System.out.println("5.去势记录");
        System.out.println("6.羊只修蹄");
        System.out.println("7.修蹄记录");
        System.out.println("8.体尺体重测量");
        System.out.println("9.羊只死亡");
        System.out.println("0.退出");
        System.out.print("请输入你的操作：");
        String opt = sc.nextLine().trim();
        switch (opt) {
            case "1":
                sheepList();
                break;
            case "2":
                addSheep();
                break;
            case "3":
                updateSheep();
                break;
            case "4":
                castrateSheep();
                break;
            case "5":
                castrateList();
                break;
            case "6":
                fixhoofAdd();
                break;
            case "7":
                fixhoofList();
                break;
            case "8":
                bodyMeasure();
                break;
            case "9":
                sheepDie();
                break;
            case "0": {
                System.out.println("你确定要退出吗(Y/N)");
                String s = sc.nextLine().trim();
                if (s.equals("Y")) {
                    return;
                } else if (s.equals("N")) {
                    break;
                } else {
                    System.out.println("输入无效");
                }
                break;
            }
            default:
                System.out.println("输入有误，请重试！");
        }
        showMainMenu();
    }

    //羊只信息列表
    public static void sheepList() {
        List<Sheep> list = sd.findAll();
        if (list.isEmpty()) {
            System.out.println("暂无数据");
            return;
        }
        for (Sheep s : list) {
            System.out.println(
                    "羊只ID:" + s.getSno() +
                            ", 性别:" + s.getSex() +
                            ", 品种:" + s.getVariety() +
                            ", 体重(kg):" + s.getBodyMeasure() +
                            ", 体况评分:" + s.getBodyScore() +
                            ", 状态:" + s.getStatus());
        }
    }

    //新增羊只
    public static void addSheep() {
        String sex;
        while (true) {
            System.out.print("性别：");
            sex = sc.nextLine().trim();
            if (!sex.isEmpty()) break;
            System.out.println("性别不能为空，请重新输入！");
        }
        String variety;
        while (true) {
            System.out.print("品种：");
            variety = sc.nextLine().trim();
            if (!variety.isEmpty()) break;
            System.out.println("品种不能为空，请重新输入！");
        }
        String measure;
        while (true) {
            System.out.print("体重(kg)：");
            measure = sc.nextLine().trim();
            if (!measure.isEmpty()) break;
            System.out.println("体重不能为空，请重新输入！");
        }
        String score;
        while (true) {
            System.out.print("体况评分：");
            score = sc.nextLine().trim();
            if (!score.isEmpty()) break;
            System.out.println("体况评分不能为空，请重新输入！");
        }
        Sheep s = new Sheep();
        s.setSex(sex);
        s.setVariety(variety);
        s.setBodyMeasure(Float.valueOf(measure));
        s.setBodyScore(Integer.valueOf(score));
        if (sd.insert(s) > 0) {
            System.out.println("新增成功");
        } else {
            System.out.println("新增失败");
        }
    }

    //修改羊只信息
    public static void updateSheep() {
        int sno;
        System.out.print("请输入要修改的羊只编号：");
        while (true) {
            String input = sc.nextLine().trim();
            if (input.isEmpty()) {
                System.out.println("羊只编号不能为空，请重新输入！");
                continue;
            }
            try {
                sno = Integer.parseInt(input);
                break;
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字！");
            }
        }

        Sheep old = sd.findById(sno);
        if (old == null) {
            System.out.println("羊只不存在");
            return;
        }
        if ("死亡".equals(old.getStatus())) {
            System.out.println("该羊只已死亡，无法操作");
            return;
        }

        System.out.print("新性别:");
        String sex = sc.nextLine();
        System.out.print("新品种:");
        String variety = sc.nextLine();
        System.out.print("新体尺:");
        String measure = sc.nextLine();
        System.out.print("新体况评分:");
        String score = sc.nextLine();

        if (!sex.isBlank()) old.setSex(sex);
        if (!variety.isBlank()) old.setVariety(variety);
        if (!measure.isBlank()) old.setBodyMeasure(Float.valueOf(measure));
        if (!score.isBlank()) old.setBodyScore(Integer.valueOf(score));

        if (sd.update(old) > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
    }

    //羊只去势
    public static void castrateSheep() {
        int sno;
        System.out.print("请输入要去势的羊只编号：");
        while (true) {
            String input = sc.nextLine().trim();
            if (input.isEmpty()) {
                System.out.println("羊只编号不能为空，请重新输入！");
                continue;
            }
            try {
                sno = Integer.parseInt(input);
                break;
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字！");
            }
        }

        Sheep sheep = sd.findById(sno);
        if (sheep == null) {
            System.out.println("羊只不存在");
            return;
        }
        if ("死亡".equals(sheep.getStatus())) {
            System.out.println("该羊只已死亡，无法操作");
            return;
        }
        if (!"公羊".equals(sheep.getSex())) {
            System.out.println("该羊只不是公羊，无法去势");
            return;
        }
        System.out.print("请输入技术员姓名：");
        String tech = sc.nextLine();
        if (sd.castrate(sno, tech) > 0) {
            System.out.println("去势成功");
        } else {
            System.out.println("去势失败");
        }
    }

    //去势列表
    public static void castrateList() {
        List<Castrate> list = cd.findAll();
        if (list.isEmpty()) {
            System.out.println("暂无数据");
            return;
        }
        for (Castrate c : list) {
            System.out.println(
                    "羊只编号：" + c.getSno() +
                            ",技术员：" + c.getTechnician() +
                            ",修蹄时间：" + c.getEventTime());
        }
    }

    //羊只修蹄
    public static void fixhoofAdd() {
        int sno;
        System.out.print("请输入要修蹄的羊只编号：");
        while (true) {
            String input = sc.nextLine().trim();
            if (input.isEmpty()) {
                System.out.println("羊只编号不能为空，请重新输入！");
                continue;
            }
            try {
                sno = Integer.parseInt(input);
                break;
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字！");
            }
        }
        Sheep sheep = sd.findById(sno);
        if (sheep == null) {
            System.out.println("羊只不存在");
            return;
        }
        if ("死亡".equals(sheep.getStatus())) {
            System.out.println("该羊只已死亡，无法操作");
            return;
        }
        System.out.print("技术员姓名：");
        String tech = sc.nextLine();
        Fixhoof f = new Fixhoof();
        f.setSno(sno);
        f.setTechnician(tech);
        if (fd.insert(f) > 0) {
            System.out.println("修蹄成功");
        } else {
            System.out.println("修蹄失败");
        }
    }

    //修蹄列表
    public static void fixhoofList() {
        List<Fixhoof> list = fd.findAll();
        if (list.isEmpty()) {
            System.out.println("暂无数据");
            return;
        }
        for (Fixhoof f : list) {
            System.out.println(
                    "羊只编号：" + f.getSno() +
                            ",技术员：" + f.getTechnician() +
                            ",修蹄时间：" + f.getEventTime());
        }
    }

    //体尺体重测量
    public static void bodyMeasure() {
        System.out.print("请输入羊只编号：");
        int sno = Integer.parseInt(sc.nextLine().trim());
        Sheep sheep = sd.findById(sno);
        if (sheep == null) {
            System.out.println("羊只不存在");
            return;
        }
        if ("死亡".equals(sheep.getStatus())) {
            System.out.println("该羊只已死亡，无法操作");
            return;
        }
        double bodyHeight = 0.0;
        double bust = 0.0;
        double wideHips = 0.0;
        double highButtocks = 0.0;
        double weight = 0.0;
        int score = 0;
        System.out.print("体高(cm)：");
        while (true) {
            try {
                bodyHeight = Double.parseDouble(sc.nextLine().trim());
                break;
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入有效的数字");
            }
        }

        System.out.print("胸围(cm)：");
        while (true) {
            try {
                bust = Double.parseDouble(sc.nextLine().trim());
                break;
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入有效的数字");
            }
        }

        System.out.print("尻宽(cm)：");
        while (true) {
            try {
                wideHips = Double.parseDouble(sc.nextLine().trim());
                break;
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入有效的数字");
            }
        }

        System.out.print("尻高(cm)：");
        while (true) {
            try {
                highButtocks = Double.parseDouble(sc.nextLine().trim());
                break;
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入有效的数字");
            }
        }

        System.out.print("体重(kg)：");
        while (true) {
            try {
                weight = Double.parseDouble(sc.nextLine().trim());
                break;
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入有效的数字");
            }
        }

        System.out.print("体况评分(1-10)：");
        while (true) {
            try {
                score = Integer.parseInt(sc.nextLine().trim());
                if (score >= 1 && score <= 10) {
                    break;
                } else {
                    System.out.print("体况评分需要在1-10之间，请重新输入：");
                }
            } catch (NumberFormatException e) {
                System.out.println("输入无效，请输入有效的数字");
            }
        }


        BodyMeasure m = new BodyMeasure();
        m.setSno(sno);
        m.setBodyHeight(bodyHeight);
        m.setBust(bust);
        m.setWideHips(wideHips);
        m.setHighButtocks(highButtocks);
        m.setWeight(weight);
        m.setBodyScore(score);

        if (bd.insert(m) > 0) {
            System.out.println("操作成功");
        } else {
            System.out.println("操作失败");
        }
    }

    //羊只死亡
    public static void sheepDie() {
        System.out.print("请输入死亡的羊只编号：");
        int sno = Integer.parseInt(sc.nextLine().trim());

        Sheep sheep = sd.findById(sno);
        if (sheep == null) {
            System.out.println("羊只不存在！");
            return;
        }
        if ("死亡".equals(sheep.getStatus())) {
            System.out.println("该羊只已死亡，无法操作");
            return;
        }

        if (sd.setDead(sno) > 0) {
            System.out.println("操作成功");
        } else {
            System.out.println("操作失败");
        }
    }
}
